package com.rongchuang.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.MemberDTO;
import com.rongchuang.entity.Member;
import com.rongchuang.entity.QMember;
import com.rongchuang.repository.MemberRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MemberService {

    @Autowired
    private JPAQueryFactory queryFactory;

    @Autowired
    private MemberRepository memberRepository;

    /**
     * 分页查询
     */
    public Page<MemberDTO> pageSearch(int pageNum, int pageSize, String memberName) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QMember member = QMember.member;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(memberName)) {
            builder.and(member.name.eq(memberName));
        }
        List<Member> members = queryFactory
                .selectFrom(member)
                .where(builder)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(member.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(member)
                .where(builder)
                .fetchCount();
        List<MemberDTO> memberDTOS = ConverterUtil.convertToList(members, MemberDTO.class);
        return new PageImpl<>(memberDTOS, pageable, total);
    }

    /**
     * 保存会员信息
     */
    public void modify(MemberDTO dto) {
        Member member;
        if (Objects.isNull(dto.getId())) {
            member = ConverterUtil.convertTo(dto, Member.class);
            member.setCreateTime(DateUtils.getCurrentTime());
        }else {
            member = memberRepository.getById(dto.getId());
            if (StringUtils.isNotBlank(dto.getAddress())) {
                member.setAddress(dto.getAddress());
            }
            if (StringUtils.isNotBlank(dto.getAssociationPost())) {
                member.setAssociationPost(dto.getAssociationPost());
            }
            if (StringUtils.isNotBlank(dto.getHeadImage())) {
                member.setHeadImage(dto.getHeadImage());
            }
            if (StringUtils.isNotBlank(dto.getIntroduction())) {
                member.setIntroduction(dto.getIntroduction());
            }
            if (StringUtils.isNotBlank(dto.getMobile())) {
                member.setMobile(dto.getMobile());
            }
            if (StringUtils.isNotBlank(dto.getName())) {
                member.setName(dto.getName());
            }
            if (Objects.nonNull(dto.getStatus())) {
                member.setStatus(dto.getStatus());
            }
        }
        member.setUpdateTime(DateUtils.getCurrentTime());
        memberRepository.save(member);
    }

    /**
     * 获取会员信息
     * @param id 会员id
     */
    public MemberDTO getDetail(Long id) {
        Member member = memberRepository.getById(id);
        return ConverterUtil.convertTo(member, MemberDTO.class);
    }
}
